{% from "dockers/dockerfile-macros.j2" import install_debian_packages, install_python_wheels, copy_files %}
ARG BASE=docker-swss-layer-bookworm-{{DOCKER_USERNAME}}:{{DOCKER_USERTAG}}

FROM $BASE AS base

ARG docker_container_name

COPY ["etc/rsyslog.conf", "/etc/rsyslog.conf"]
RUN [ -f /etc/rsyslog.conf ] && sed -ri "s/%syslogtag%/$docker_container_name#%syslogtag%/;" /etc/rsyslog.conf

## Make apt-get non-interactive
ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update && apt-get install -y gnupg

RUN apt-get install -y net-tools \
                       iproute2 \
                       arping \
                       ndisc6 \
                       ethtool \
                       tcpdump \
                       ifupdown \
                       bridge-utils \
                       libjudydebian1 \
                       openssh-client \
                       openssh-server \
                       libc-ares2 \
                       bash-completion \
                       logrotate \
                       apt-utils \
                       psmisc \
                       python3-scapy \
                       conntrack \
                       iptables \
                       jq \
                       uuid-dev \
                       # For installing Python m2crypto package
                       # (these can be uninstalled after installation)
                       build-essential \
                       python3-dev \
                       python3-gi \
                       libssl-dev \
                       swig \
                       # For using Python m2crypto package
                       openssl \
                       # For installing dependent Python packages of sonic-host-services
                       # (these can be uninstalled after installation)
                       libcairo2-dev \
                       libdbus-1-dev \
                       libgirepository1.0-dev \
                       libsystemd-dev \
                       pkg-config \
                       # For installing dependent Python packages of sonic-host-services
                       # these packages are needed at runtime
                       gir1.2-glib-2.0 \
                       libdbus-1-3 \
                       libgirepository-1.0-1 \
                       {%- if ENABLE_ASAN == "y" %}
                       libasan6 \
                       {%- endif %}
                       dbus \
                       redis-server \
                       # For libkrb5-dev
                       comerr-dev \
                       libgssrpc4 \
                       libkdb5-10 \
                       libprotobuf-c-dev \
                       protobuf-c-compiler

# For sonic-config-engine Python 3 package
# Install pyangbind here, outside sonic-config-engine dependencies, as pyangbind causes enum34 to be installed.
# Then immediately uninstall enum34, as enum34 should not be installed for Python >= 3.4, as it causes a
# conflict with the new 'enum' module in the standard library
# https://github.com/robshakir/pyangbind/issues/232
RUN pip3 install pyangbind==0.8.1
RUN pip3 uninstall -y enum34

# Dependencies of restore_neighbors.py
RUN pip3 install \
         pyroute2==0.7.2 \
         netifaces==0.11.0

{% if docker_sonic_vs_debs.strip() -%}
# Copy built Debian packages
{{ copy_files("debs/", docker_sonic_vs_debs.split(' '), "/debs/") }}

# Install built Debian packages and implicitly install their dependencies
{{ install_debian_packages(docker_sonic_vs_debs.split(' ')) }}
{%- endif %}

RUN apt-get install -y libzmq3-dev

{% if docker_sonic_vs_pydebs.strip() -%}
# Copy built Debian packages
{{ copy_files("python-debs/", docker_sonic_vs_pydebs.split(' '), "/debs/") }}

# Install built Debian packages and implicitly install their dependencies
{{ install_debian_packages(docker_sonic_vs_pydebs.split(' ')) }}
{%- endif %}

{% if docker_sonic_vs_whls.strip() %}
# Copy locally-built Python wheel dependencies
{{ copy_files("python-wheels/", docker_sonic_vs_whls.split(' '), "/python-wheels/") }}

# Install locally-built Python wheel dependencies
{{ install_python_wheels(docker_sonic_vs_whls.split(' ')) }}
{% endif %}

# Clean up
RUN apt-get purge -y build-essential libssl-dev swig
RUN apt-get purge -y python3-dev
RUN apt-get purge -y libcairo2-dev libdbus-1-dev libgirepository1.0-dev libsystemd-dev pkg-config
RUN apt-get clean -y
RUN apt-get autoclean -y
RUN apt-get autoremove -y
RUN rm -rf /debs ~/.cache

RUN sed -ri 's/^# save ""$/save ""/g;                    \
             s/^daemonize yes$/daemonize no/;            \
             s/^logfile .*$/logfile ""/;                 \
             s/^# syslog-enabled no$/syslog-enabled no/; \
             s/^# unixsocket/unixsocket/;                \
             s/redis-server.sock/redis.sock/g;           \
             s/^client-output-buffer-limit pubsub [0-9]+mb [0-9]+mb [0-9]+/client-output-buffer-limit pubsub 0 0 0/; \
             s/^notify-keyspace-events ""$/notify-keyspace-events AKE/; \
             s/^databases [0-9]+$/databases 100/ \
            ' /etc/redis/redis.conf

COPY ["50-default.conf", "/etc/rsyslog.d/"]
COPY ["start.sh", "orchagent.sh", "files/update_chassisdb_config", "/usr/bin/"]
COPY ["supervisord.conf.j2", "/usr/share/sonic/templates/"]
COPY ["files/configdb-load.sh", "/usr/bin/"]
COPY ["files/arp_update", "/usr/bin/"]
COPY ["files/buffers_config.j2", "files/qos_config.j2", "files/arp_update_vars.j2", "files/copp_cfg.j2", "/usr/share/sonic/templates/"]
COPY ["files/sonic_version.yml", "/etc/sonic/"]
COPY ["port_breakout_config_db.json", "/etc/sonic/"]
COPY ["database_config.json", "/etc/default/sonic-db/"]
COPY ["hostname.j2", "/usr/share/sonic/templates/"]
COPY ["init_cfg.json.j2", "/usr/share/sonic/templates/"]
COPY ["default_chassis_cfg.json", "/etc/default/sonic-db/"]
COPY ["asic_table.json", "/etc/sonic/"]
COPY ["zero_profiles.json", "/etc/sonic"]
COPY ["buffermgrd.sh", "/usr/bin/"]

COPY ["platform.json", "/usr/share/sonic/device/x86_64-kvm_x86_64-r0/"]
COPY ["platform-dpu-2p.json", "/usr/share/sonic/device/x86_64-kvm_x86_64-r0/"]
COPY ["hwsku.json", "/usr/share/sonic/device/x86_64-kvm_x86_64-r0/Force10-S6000/"]
COPY ["hwsku.json", "/usr/share/sonic/device/x86_64-kvm_x86_64-r0/brcm_gearbox_vs/"]
COPY ["hwsku.json", "/usr/share/sonic/device/x86_64-kvm_x86_64-r0/Mellanox-SN2700/"]

RUN mkdir -p /etc/supervisor/conf.d/
RUN sonic-cfggen -a "{\"ENABLE_ASAN\":\"{{ENABLE_ASAN}}\"}" -t /usr/share/sonic/templates/supervisord.conf.j2 > /etc/supervisor/conf.d/supervisord.conf
RUN rm -f /usr/share/sonic/templates/supervisord.conf.j2

{%- if ENABLE_ASAN == "y" %}
RUN mkdir -p /var/log/asan
{%- endif %}

RUN echo "docker-sonic-vs" > /etc/hostname
RUN mkdir -p /etc/quagga
RUN touch /etc/quagga/zebra.conf

# disable integrated vtysh config
RUN rm /etc/frr/frr.conf

COPY ["frr/zebra.conf", "/etc/frr/"]

# Create /var/warmboot/teamd folder for teammgrd
RUN mkdir -p /var/warmboot/teamd

FROM $BASE

RUN --mount=type=bind,from=base,target=/changes-to-image rsync -axAX --no-D --exclude=/sys --exclude=/proc --exclude=/dev --exclude=resolv.conf /changes-to-image/ /

# Set PLATFORM and HWSKU environment variables
ENV PLATFORM=x86_64-kvm_x86_64-r0
ENV HWSKU=Force10-S6000

ENTRYPOINT ["/usr/local/bin/supervisord"]
